<?php

/**
 * model_file
 */

class Model_file extends YOU {

	public function __construct()
	{

		parent::__construct();
	}

	/** 文件上传 **/
	public function upload($folder = "", $id = 0, $type_ini = array() )
	{
		
		if (empty($folder)){
			exit("folder错误");
		}

		// 上传附件
		if (!empty($_FILES[$folder]['tmp_name'])){

			if (function_exists(finfo_open)){
				$finfo    = finfo_open(FILEINFO_MIME);
				$mimetype = finfo_file($finfo, $_FILES[$folder]['tmp_name']);
				finfo_close($finfo);

				$ftype = $this->model->file->getMime($mime);
			}else{
				$a = explode(".", $_FILES[$folder]['name']);
				$ftype = strtolower($a[count($a)-1]);
			}

			if (!empty($type_ini)){
				if(!$this->checkType($ftype, $type_ini)){
					Helper_Client::alert("上传文件格式非法");
				}
			}
			
			$upload_dir = UPLOAD_PATH."/{$folder}";
			// 没有目录则创建目录
			$this->make_attach_dir($upload_dir);

			$fname = "{$id}.{$ftype}";
			$file_path = "{$upload_dir}/{$fname}";

			copy($_FILES[$folder]['tmp_name'], $file_path);


			$info = array(
			"fname"  => $fname,
			"fsize"  => $_FILES[$folder]['size'],
			"fpath"  => $file_path,
			"ftype"  => $ftype,
			"ftitle" => $_FILES[$folder]['name'],
			);
		
		}

		return $info;

	}

	// 得到文件头信息
	public function getMime($mime = ""){

		// 图片文件类型
		$mime_ini = array(
		'image/bmp'  => 'bmp',
		'image/gif'  => 'gif',
		'image/ief'  => 'ief',
		'image/jpeg' => 'jpg',
		'image/png'  => 'png',
		'text/css'   => 'css',
		'text/html'  => 'htm',
		'text/plain' => 'txt',
		'text/richtext'             => 'rtx',
		'text/rtf'                  => 'rtf',
		'text/sgml'                 => 'sgm',
		'text/tab-separated-values' => 'tsv',
		'text/vnd.wap.wml'          => 'wml',
		'text/vnd.wap.wmlscript'    => 'wmls',
		'text/x-setext'             => 'etx',
		'text/xml'                  => 'xml',
		'video/mpeg'                => 'mpe',
		'video/quicktime'           => 'mov',
		'video/vnd.mpegurl'         => 'mxu',
		'video/x-msvideo'           => 'avi',
		'video/x-sgi-movie'         => 'movie',
		'x-conference/x-cooltalk'   => 'ice',
		'application/msword'        => '.doc',
		'application/vnd.openxmlformats-officedocument.wordprocessingml.document'   => 'docx',
		'application/rtf'                                                           => 'rtf',
		'application/vnd.ms-excel'                                                  => 'xls',
		'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'         => 'xlsx',
		'application/vnd.ms-powerpoint'                                             => 'ppt',
		'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
		'application/vnd.ms-powerpoint'                                             => 'pps',
		'application/vnd.openxmlformats-officedocument.presentationml.slideshow'    => 'ppsx',
		'application/pdf'               => 'pdf',
		'application/x-shockwave-flash' => 'swf',
		'application/x-msdownload'      => 'dll',
		'application/octet-stream'      => 'exe',
		'application/octet-stream'      => 'msi',
		'application/octet-stream'      => 'chm',
		'application/octet-stream'      => 'cab',
		'application/octet-stream'      => 'ocx',
		'application/octet-stream'      => 'rar',
		'application/x-tar'             => 'tar',
		'application/x-compressed'      => 'tgz',
		'application/x-zip-compressed'  => 'zip',
		);

		if (!empty($mime_ini[$mime])){
			return $mime_ini[$mime];
		}else{
			return false;
		}

	}

	// 检查文件类型
	public function checkType($ftype = "", $prv = array() ){
		foreach ($prv as $key => $value){
			if ($value == $ftype){
				return true;
			}
		}

		return false;
	}

	
	/** 得到文件目录名 **/
	public function getFolderName($folder_id)
{
	$folder_data = $this->db->fetchRow("SELECT folder_name FROM `tbl_file_folder` WHERE folder_id='{$folder_id}'");

	return $folder_data['folder_name'];
	
}
	
	
	
	function fileext($file)
	{
		return pathinfo($file, PATHINFO_EXTENSION);
	}
	


	/** 递归创建目录 **/
	function make_attach_dir($path, $mode = 0777)
	{
		if (is_dir($path)) {
			if (!(is_writable($path))) {
				@chmod($path, $mode);
			}
			return true;
		} else {
			$oldmask = @umask(0);
			$partialpath = dirname($path);
			if (!self::make_attach_dir($partialpath, $mode)) {
				return false;
			} else {
				return mkdir($path, $mode);
			}
		}
	}

	function formatBytes($size) {
		$units = array(' B', ' KB', ' MB', ' GB', ' TB');
		for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
		return round($size, 2).$units[$i];
	}
	// 下载次数
	function getip()
	{
		if (isset($_SERVER))
		{
			if (isset($_SERVER[HTTP_X_FORWARDED_FOR]) && strcasecmp($_SERVER[HTTP_X_FORWARDED_FOR], "unknown"))//代理
			{
				$realip = $_SERVER[HTTP_X_FORWARDED_FOR];
			}
			elseif(isset($_SERVER[HTTP_CLIENT_IP]) && strcasecmp($_SERVER[HTTP_CLIENT_IP], "unknown"))
			{
				$realip = $_SERVER[HTTP_CLIENT_IP];
			}
			elseif(isset($_SERVER[REMOTE_ADDR]) && strcasecmp($_SERVER[REMOTE_ADDR], "unknown"))
			{
				$realip = $_SERVER[REMOTE_ADDR];
			}
			else
			{
				$realip = 'unknown';
			}
		}
		else
		{
			if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
			{
				$realip = getenv("HTTP_X_FORWARDED_FOR");
			}
			elseif(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
			{
				$realip = getenv("HTTP_CLIENT_IP");
			}
			elseif(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
			{
				$realip = getenv("REMOTE_ADDR");
			}
			else
			{
				$realip = 'unknown';
			}
		}
		return $realip;
	}
}

?>